package com.biz.primus.model.coupon.vo.couponType;

import com.biz.primus.base.exception.utils.AssertUtils;
import com.biz.primus.common.support.jackson.PrimusLocalDateTimeDeserializer;
import com.biz.primus.common.support.jackson.PrimusLocalDateTimeSerializer;
import com.biz.primus.common.utils.ValueUtils;
import com.biz.primus.model.coupon.enums.CouponLimitType;
import com.biz.primus.model.coupon.enums.CouponScopeType;
import com.biz.primus.model.coupon.exceptions.CouponTypeExceptionType;
import com.biz.primus.model.coupon.interfaces.IModelValidation;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

/**
 * 优惠券类型Vo
 *
 * @author david-liu
 */
@Getter
@Setter
@ApiModel
public class CouponTypeVo implements Serializable, IModelValidation {
    private static final long serialVersionUID = -1796148428678642698L;

    //券类型名称
    @ApiModelProperty(name = "couponTypeName", notes = "券类型名称", required = true)
    protected String couponTypeName;

    //优惠券使用范围类型(平台券/商家券)
    @ApiModelProperty(name = "couponScopeType", notes = "优惠券使用范围类型(平台券/商家券), PLATFORM: 平台券, VENDOR: 商家券, 默认: PLATFORM")
    protected CouponScopeType couponScopeType = CouponScopeType.PLATFORM;

    //限制可以使用优惠券的分类id,如果为null或空set,则表示不限制分类
    @ApiModelProperty(name = "categories", notes = "限制可以使用优惠券的分类id")
    protected List<Long> categories;

    //限制可以使用优惠券的品牌id,如果为null或空set,则表示不限制品牌
    @ApiModelProperty(name = "brands", notes = "限制可以使用优惠券的品牌id")
    protected List<Long> brands;

    //限制可以使用的商品id
    @ApiModelProperty(name = "includeProducts", notes = "限制可以使用的商品id")
    protected List<Long> includeProducts;

    //限制不可以使用优惠券的商品id,与includeProducts二选一填写,如果都为空,则不限制商品
    @ApiModelProperty(name = "excludeProducts", notes = "限制不可以使用优惠券的商品id")
    protected List<Long> excludeProducts;

    //限制可以使用优惠券的商家
    @ApiModelProperty(name = "vendors", notes = "限制可以使用优惠券的商家")
    protected List<Long> vendors;

    //优惠券限制使用类型
    @ApiModelProperty(name = "couponLimitType", notes = "优惠券限制使用类型, 默认: ALL")
    protected CouponLimitType couponLimitType = CouponLimitType.ALL;

    //优惠券类型介绍
    @ApiModelProperty(name = "couponTypeDesc", notes = "优惠券类型介绍")
    protected String couponTypeDesc;

    //优惠券面值
    @ApiModelProperty(name = "faceValue", notes = "优惠券面值, 单位: 分", required = true)
    protected Integer faceValue;

    //优惠券门槛
    @ApiModelProperty(name = "couponLimitAmount", notes = "优惠券门槛, 单位: 分; 如果为空或者为0, 表示无限制", required = true)
    protected Integer couponLimitAmount;

    //优惠券可用开始时间
    @JsonDeserialize(using = PrimusLocalDateTimeDeserializer.class)
    @JsonSerialize(using = PrimusLocalDateTimeSerializer.class)
    @ApiModelProperty(name = "usableStartTime", notes = "优惠券可用开始时间", required = true, example = "2017-09-01 00:00:00")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    protected LocalDateTime usableStartTime;

    //优惠券可用终止时间
    @JsonDeserialize(using = PrimusLocalDateTimeDeserializer.class)
    @JsonSerialize(using = PrimusLocalDateTimeSerializer.class)
    @ApiModelProperty(name = "usableEndTime", notes = "优惠券可用结束时间", required = true, example = "2017-09-10 00:00:00")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    protected LocalDateTime usableEndTime;

    //优惠券可领开始时间
    @JsonDeserialize(using = PrimusLocalDateTimeDeserializer.class)
    @JsonSerialize(using = PrimusLocalDateTimeSerializer.class)
    @ApiModelProperty(name = "receivableStartTime", notes = "优惠券可领开始时间", required = true, example = "2017-09-10 00:00:00")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    protected LocalDateTime receivableStartTime;

    //优惠券可领终止时间
    @JsonDeserialize(using = PrimusLocalDateTimeDeserializer.class)
    @JsonSerialize(using = PrimusLocalDateTimeSerializer.class)
    @ApiModelProperty(name = "receivableEndTime", notes = "优惠券可领终止时间", required = true, example = "2017-09-10 00:00:00")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    protected LocalDateTime receivableEndTime;

    //优惠券领取开始之后, 几天后优惠券不可用
    @ApiModelProperty(name = "usableCountDownDays", notes = "优惠券领取开始之后, 几天后优惠券不可用, 如果为空, 默认: 2147483647")
    protected Integer usableCountDownDays = Integer.MAX_VALUE;

    //券类型发行总数 不可修改
    @ApiModelProperty(name = "totalQuantity", notes = "券发行总数; 设置之后, 不可修改", required = true)
    protected Integer totalQuantity;

    // 优惠券是否启用
    @ApiModelProperty(name = "enable", notes = "优惠券是否启用, 默认: true", required = true)
    protected Boolean enable = true;

    // 优惠券图片, 如果需要使用图片, 赋值
    @ApiModelProperty(name = "icon", notes = "优惠券图片; 如果未传, 使用默认的优惠券图片")
    protected String icon;

    // 优惠券备注信息
    @ApiModelProperty(name = "remark", notes = "优惠券备注信息")
    protected String remark;

    // 优惠券类型创建人
    @ApiModelProperty(name = "creator", notes = "优惠券类型创建人")
    protected String creator;

    // 优惠券类型更新人
    @ApiModelProperty(name = "lastUpdateUser", notes = "优惠券类型更新用户")
    protected String lastUpdateUser;

    @Override
    public void validate() {
        AssertUtils.hasText(couponTypeName, CouponTypeExceptionType.PARAMETER_ILLEGAL, new Object[]{"couponTypeName不能为空"});
        AssertUtils.isTrue(ValueUtils.getValue(faceValue) > 0, CouponTypeExceptionType.PARAMETER_ILLEGAL, new Object[]{"faceValue不能为空"});
        AssertUtils.notNull(couponLimitAmount, CouponTypeExceptionType.PARAMETER_ILLEGAL, new Object[]{"couponLimitAmount不能为空"});
        AssertUtils.isTrue(Objects.nonNull(usableStartTime) && Objects.nonNull(usableEndTime), CouponTypeExceptionType.PARAMETER_ILLEGAL, new Object[]{"usableStartTime, usableEndTime不能为空, 格式为yyyy-MM-dd HH:mm:ss"});
        AssertUtils.isTrue(Objects.nonNull(receivableStartTime) && Objects.nonNull(receivableEndTime), CouponTypeExceptionType.PARAMETER_ILLEGAL, new Object[]{"receivableStartTime, receivableEndTime不能为空, 格式为yyyy-MM-dd HH:mm:ss"});
        AssertUtils.isTrue(ValueUtils.getValue(totalQuantity) > 0, CouponTypeExceptionType.PARAMETER_ILLEGAL, new Object[]{"totalQuantity不能为空"});
        AssertUtils.isTrue(StringUtils.isNotBlank(creator) && StringUtils.isNotBlank(lastUpdateUser), CouponTypeExceptionType.PARAMETER_ILLEGAL, new Object[]{"creator, lastUpdateUser不能为空"});
    }
}
